// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// N [author] rhyme_lph
// c [email]  rhymelph@gmail.com
// h [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/company_entity.dart';
import 'package:finance_app/src/entities/sub_billing_item_entity.dart';
import 'package:finance_app/src/http_service/http_company_service.dart';
import 'package:finance_app/src/provider/setting/subscription_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/judge_bottom_sheet.dart';

class SubBillingProvider extends BaseProvider {
  List<SubBillingItemEntity> upcomingBills = [];

  @override
  Future get dataFuture => null;

  @override
  void initState() {
    super.initState();
  }

  void setUpcomingBills(List<SubBillingItemEntity> billingList) {
    this.upcomingBills = billingList;
    update();
  }

  void onNavBillDetail(SubBillingItemEntity value) {
    Routes.navigateTo(RouterGenProviders.subInvoicePage, params: {
      'id': value.id,
    });
  }

  void onSelectCompany(SubBillingItemEntity bill) async {
    if (Config.currentCompany.id != bill.companyId) {
      bool isYes = false;
      obtainContext((context) async {
        isYes = await showSwitchCompanyJudgeBottomSheet(context);
        if (isYes) {
          getParentProvider<SubscriptionProvider>()
              .onChangeCompany(bill.companyId);
        }
      });

      return;
    }

    if (bill.originalPrice == 0) {
      getParentProvider<SubscriptionProvider>().controller.animateTo(1);
    } else {
      final result =
          await Routes.navigateTo(RouterGenProviders.subCompanyPage, params: {
        'subBillingItem': bill,
      });
      if (result == true) {
        getParentProvider<SubscriptionProvider>().onTry();
      }
    }
  }
}
